package com.dq.controller.app.user;

import com.alibaba.fastjson.JSONObject;
import com.dq.context.Constant;
import com.dq.controller.in.CountAccountQuery;
import com.dq.controller.in.StaffSubscribeQuery;
import com.dq.controller.in.user.SysUserQuery;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.sys.role.SysRole;
import com.dq.domain.sys.role.SysUserRole;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.service.subscribe.SubscribeService;
import com.dq.utils.TimeUtil;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */

@RestController
@RequestMapping("/app/orguser/")
@Api(description = "门店用户管理")
public class AppOrgUserController extends AllService {

//    单个门店
    @PostMapping("userListAll")
    @ApiOperation("门店所有用户列表-店长/员工")
    public Vo userListAll(@RequestBody SysUserQuery sysUserQuery)throws AdminException {
        sysUserQuery.format();
        sysUserQuery.setPage(null);
        sysUserQuery.setSize(null);
        List<BossUser> list = orgUserService.getOrgUserListByRoles(sysUserQuery, 3, 4);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list));
    }
    @PostMapping("userList")
    @ApiOperation("门店所有用户列表-分页")
    public Vo userList(@RequestBody SysUserQuery sysUserQuery)throws AdminException {
        sysUserQuery.format();

        int[] ints = {3, 4};
        List<BossUser> list = orgUserService.getOrgUserListByRoles(sysUserQuery, ints);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list), orgUserService.getOrgUserCountByRoles(sysUserQuery, ints));
    }

// 所有门店
    @PostMapping("userShopListAll")
    @ApiOperation("所有门店的店长列表")
    public Vo userShopListAll(@RequestBody SysUserQuery sysUserQuery)throws AdminException {
        sysUserQuery.format();
        sysUserQuery.setPage(null);
        sysUserQuery.setSize(null);

        List<BossUser> list = bossUserService.getBossUserListByRoles(sysUserQuery, 3);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list));
    }

    @PostMapping("loginInfo")
    @ApiOperation("门店登录账号-我的信息(用户信息)")
    public Vo loginInfo()throws AdminException {
        SysUser loginUser = sysUserService.getUserInSection();
        if(!(loginUser instanceof BossUser)) {
            throw new AdminException("登录账号非员工/门店账号");
        }
        BossUser bossUser = (BossUser) loginUser;

        BossUser.Vo bossUserVo = (BossUser.Vo) bossUser.toVO();
        List<SysUserRole> userRoles = sysUserRoleRepository.findByDeletedFalseAndSysUserAndSysRoleStatus(bossUser, SysRole.Status.on);
        List<Long> roleId = new ArrayList<>();
        List<String> roleName = new ArrayList<>();
        List<String> roleCode = new ArrayList<>();
        if(userRoles != null && userRoles.size() > 0) {
            for (SysUserRole userRole : userRoles) {
                if(userRole.getSysRole().getCode().equals(Constant.ROLE_CODE_LIST.get(3))) {
                    bossUserVo.setRoleShowName(userRole.getSysRole().getRoleName());
                    break;
                }else if(userRole.getSysRole().getCode().equals(Constant.ROLE_CODE_LIST.get(4))) {
                    bossUserVo.setRoleShowName(userRole.getSysRole().getRoleName());
                    break;
                }
            }
            for (SysUserRole userRole : userRoles) {
                roleId.add(userRole.getSysRole().getId());
                roleName.add(userRole.getSysRole().getRoleName());
                roleCode.add(userRole.getSysRole().getCode());
            }
        }
        bossUserVo.setRoleId(roleId);
        bossUserVo.setRoleName(roleName);
        bossUserVo.setRoleCode(roleCode);
        return new Vo(bossUserVo);
    }
    @PostMapping("loginInfo/statistics")
    @ApiOperation("门店登录账号-我的信息(根据时间展示统计总数详情)")
    public Vo loginInfoStatistics(@ApiParam("需要参数: beginTime/endTime; 当登录账号为部门经理/省总时,需要参数 orgId") StaffSubscribeQuery staffSubscribeQuery)throws AdminException {
        SysUser loginUser = sysUserService.getUserInSection();
        if(!(loginUser instanceof BossUser)) {
            throw new AdminException("登录账号非员工/门店账号");
        }
        BossUser bossUser = (BossUser) loginUser;

        if(staffSubscribeQuery.getOrgId() == null) {
            staffSubscribeQuery.setOrgId(bossUser.getSection().getId());
        }

        List<SysUserRole> userRoles = sysUserRoleRepository.findByDeletedFalseAndSysUserAndSysRoleStatus(bossUser, SysRole.Status.on);
        if(userRoles != null && userRoles.size() > 0) {
            for (SysUserRole userRole : userRoles) {
                if(userRole.getSysRole().getCode().equals(Constant.ROLE_CODE_LIST.get(3))) {
                    break;
                }else if(userRole.getSysRole().getCode().equals(Constant.ROLE_CODE_LIST.get(4))) {
                    staffSubscribeQuery.setStaffUserId(bossUser.getId());
                    break;
                }
            }
        }
        Long todayBegin = TimeUtil.getTodayZeroTimestamp();
        Long todayEnd = TimeUtil.getTodayEndTimestamp();
        if(staffSubscribeQuery.getBeginTime() == null) {
            staffSubscribeQuery.setBeginTime(todayBegin);
        }
        if(staffSubscribeQuery.getEndTime() == null) {
            staffSubscribeQuery.setEndTime(todayEnd);
        }
        // 预约数
        Long subscribeAllCount = subscribeService.subscribeCount(staffSubscribeQuery);
        staffSubscribeQuery.setSubscribeStatus(Subscribe.SubscribeStatus.serviced);
        Long subscribeOverCount = subscribeService.subscribeCount(staffSubscribeQuery);
        // todo uu 本日充值业绩/本日消耗业绩
        BigDecimal todayExpend = subscribeService.countSubscribe(staffSubscribeQuery);
        staffSubscribeQuery.setSubscribeStatus(null);
        BigDecimal todayTopUp = accountService.countAccountByToDay(staffSubscribeQuery);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("subscribeAllCount", subscribeAllCount);
        jsonObject.put("subscribeOverCount", subscribeOverCount);
        jsonObject.put("todayTopUp", todayTopUp);
        jsonObject.put("todayExpend", todayExpend);

        return new Vo(jsonObject);
    }

}
